package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import saucaca.Object.SingerObject;
import saucaca.fragment.ArtistDetailFragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class ArtistAdapter extends BaseAdapter {
	Context context;
	private String tag;
	private LayoutInflater inflater;
	private List<SingerObject> list;
	ImageLoader imageLoader;

	public void update(List<SingerObject> _list) {
		list = _list;
		notifyDataSetChanged();
	}

	public ArtistAdapter(Context context, List<SingerObject> _list) {
		list = _list;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		imageLoader=((BaseActivity)context).imageLoader;
	}

	public String getTag() {
		return tag;
	}

	public void setList(ArrayList<SingerObject> _list, String _tag) {
		list = _list;
		this.tag = _tag;
		notifyDataSetChanged();
	}

	public List<SingerObject> getList() {
		return list;
	}

	class ViewHolder {
		RelativeLayout layout;
		ImageView imgeAvatar;
		TextView textViewName;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_artist_listartist, null);
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.artist_item_layout);
			holder.imgeAvatar = (ImageView) convertView.findViewById(R.id.artist_picture);
			holder.textViewName = (TextView) convertView.findViewById(R.id.artist_txtName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SingerObject singerObject = list.get(position);
				((MainActivity) context).switchContent(ArtistDetailFragment.newInstance(singerObject));
			}
		});
		holder.textViewName.setText(list.get(position).getTXNAME());
		imageLoader.get(list.get(position).getAVATAR(), ImageLoader.getImageListener(holder.imgeAvatar,
			         R.drawable.ic_default, R.drawable.ic_default));
		return convertView;
	}

}
